include("shared.lua")
include("cl_mshud.lua")
include("cl_ms_scoreboard.lua")
include("cl_ms_pickteam.lua")

DEFINE_BASECLASS( "gamemode_base" )

function GM:Initialize()
	CRoundState = 0
	CRoundTimer = 0
	CRoundResult = 0
	
	CMod1 = "---"
	CMod2 = "---"
	CMod3 = "---"
	
	NotifFeed = {}
	
	CVAR_WallhacksGlowDisable = CreateClientConVar( "metastrike_whglow_disable", 0, true, false )
end

function GM:Think()
	
end

local function UpdateRoundState()
	CRoundState = GetGlobalInt("ms_rndstate", 0)
	CRoundTimer = GetGlobalInt("ms_rndstate", 0)
	CRoundResult = GetGlobalInt("ms_rndresult", 0)
end
usermessage.Hook("updateroundstate", UpdateRoundState)

local function UpdateMSMods()
	CMod1 = GetGlobalString("ms_mod1", "---")
	CMod2 = GetGlobalString("ms_mod2", "---")
	CMod3 = GetGlobalString("ms_mod3", "---")
	CMaxHealth = GetGlobalInt("ms_maxhp",200)
end
usermessage.Hook("updatemsmods", UpdateMSMods)

local function TeamSelect(ply, cmd, args)
   GAMEMODE:ShowTeam()
end
concommand.Add("metastrike_teamselect", TeamSelect)

local function GetNotif( message )
	local notif = {}
	local notifType = message:ReadString()
	if notifType == "PlayerKilledPlayer" then
		notif.ntype = "PlayerKilledPlayer"
		local attacker = message:ReadEntity()
		local inflictor = message:ReadString()
		local victim = message:ReadEntity()
		if attacker==LocalPlayer() or victim==LocalPlayer() then 
			notif.time = CurTime()+10
			notif.localplayer = true
		else notif.time = CurTime()+6
		end
		notif.attacker 			= 	attacker:Name()
		notif.attacker_color	=	team.GetColor(attacker:Team())
		notif.inflictor 		= 	inflictor
		notif.victim			= 	victim:Name()
		notif.victim_color		=	team.GetColor(victim:Team())
	elseif notifType == "PlayerSpecialAction" then
		notif.ntype = "PlayerSpecialAction"
		local nplayer = message:ReadEntity()
		local action = message:ReadString()
		notif.localplayer=false
		if nplayer==LocalPlayer() then 
			notif.time = CurTime()+12
			notif.localplayer=true
		else notif.time = CurTime()+8
		end
		notif.nplayer 			= 	nplayer:Name()
		notif.nplayer_color		=	team.GetColor(nplayer:Team())
		notif.action 			= 	action
	elseif notifType == "SpecialKilledPlayer" then
		notif.ntype = "SpecialKilledPlayer"
		local attacker = message:ReadString()
		local victim = message:ReadEntity()
		notif.localplayer=false
		if victim==LocalPlayer() then 
			notif.time = CurTime()+10
			notif.localplayer=true
		else notif.time = CurTime()+6
		end
		notif.attacker 			= 	attacker
		notif.victim			= 	victim
		notif.victim_color		=	team.GetColor(victim:Team())
	end
	table.insert(NotifFeed,notif)
end
usermessage.Hook("MSNotice", GetNotif)

local function MSHalos()
	if GAMEMODE:MSCheckForMod("Wallhacks") and GetConVarNumber( "metastrike_whglow_disable" )==0 then
		local adminglows = {}
		local mingeglows = {}
		for k,v in pairs(team.GetPlayers(TEAM_ADMIN)) do if v:Alive() then table.insert(adminglows,v) end end
		for k,v in pairs(team.GetPlayers(TEAM_MBAGS)) do if v:Alive() then table.insert(mingeglows,v) end end
		halo.Add( adminglows, team.GetColor(TEAM_ADMIN), 5,5,2,true,true)
		halo.Add( mingeglows, team.GetColor(TEAM_MBAGS), 5,5,2,true,true)
	end
end

hook.Add( "PreDrawHalos", "MSHalos", MSHalos )

function GM:MSAutoAssign()
	local ID = table.Random({TEAM_ADMIN, TEAM_MBAGS})
	local plcnt = {}
	plcnt[TEAM_ADMIN] = #team.GetPlayers(TEAM_ADMIN)
	plcnt[TEAM_MBAGS] = #team.GetPlayers(TEAM_MBAGS)
	if plcnt[LocalPlayer():Team()] then plcnt[LocalPlayer():Team()]=plcnt[LocalPlayer():Team()]-1 end
	if plcnt[TEAM_ADMIN]>plcnt[TEAM_MBAGS] then ID = TEAM_MBAGS end
	if plcnt[TEAM_MBAGS]>plcnt[TEAM_ADMIN] then ID = TEAM_ADMIN end
	RunConsoleCommand( "changeteam", ID )
end

function GM:PostDrawOpaqueRenderables( bDrawingDepth, bDrawingSkybox )
	CMaxHealth = GetGlobalInt("ms_maxhp",100)
	if self:MSCheckForMod("Healthbars") then
		for k,v in pairs(player.GetAll()) do
		if v~=LocalPlayer() and v:Alive() then
			local pos = v:GetPos()
			local ang = LocalPlayer():EyeAngles()
			pos = pos + (v:GetAngles():Up()*80)
			ang:RotateAroundAxis( ang:Forward(), 90 )
			ang:RotateAroundAxis( ang:Right(), 90 )
			ang = Angle(0, ang.y, ang.roll)
			cam.Start3D2D(pos,ang,0.5)
				surface.SetDrawColor(0,0,0)
				surface.DrawRect(-51,-1,102,12)
				surface.SetDrawColor(50,250,50)
				surface.DrawRect(-50,0,100*(v:Health()/(CMaxHealth)),10)
			cam.End3D2D()
		end
	end
	end
end